#!/usr/bin/python3.8
# -*- coding: utf-8 -*-
# @Time    : 2021/3/6 5:55 下午 
# @Author  : zhaohexin
# @Email   : 1483643520@xxx.com
# @File    : crud.py
# @Software: PyCharm 
# @FileDescription：岗位具体查询逻辑

from sqlalchemy.orm import Session

from apps.system.jobs import model
from apps.system.users import model as userModel


def get_job(db: Session, job_id: int):
    """
    获取单条岗位数据
    :param db:
    :param job_id:
    :return:
    """
    return db.query(model.Jobs).filter(model.Jobs.id == job_id).first()


def get_jobs(db: Session, page: int, size: int, sort: str = "sort"):
    """
    获取全部岗位数据
    :param db: 
    :param page: 
    :param size: 
    :param sort: 
    :return: 
    """
    offset = page * size
    return db.query(model.Jobs).order_by(sort).offset(offset).limit(size).all()


def get_jobs_by_user(db: Session, user_id: int):
    """
    根据所属用户查询岗位
    :param db:
    :param user_id:
    :return:
    """
    jobs = [job.job_id for job in db.query(userModel.UserJobs).filter(userModel.UserJobs.user_id == user_id)]
    return db.query(model.Jobs).filter(model.Jobs.id.in_(jobs)).all()
